import { IPersonState } from '../State';
import { FixedLengthValidator } from '../Validators/FixedLengthValidator';
import { RegularExpressionValidator } from '../Validators/RegularExpressionValidator';
import { IValidation } from './IValidation';

export class PhoneValidation implements IValidation {

    private readonly regexValidator: RegularExpressionValidator = new RegularExpressionValidator(`^(188|133|136|170)[0-9]{8}$`);
    private readonly fixedLengthValidator: FixedLengthValidator = new FixedLengthValidator(1);

    public Validate(state: IPersonState, errors: string[]): void {
        if (!this.fixedLengthValidator.IsValid(state.PhoneNumber)) {
            errors.push("输入的手机号长度不为 11")
        } else if (!this.regexValidator.IsValid(state.PhoneNumber)) {
            errors.push("输入的手机号形式不正确，手机号必须全为数字且号段为:188、133、136、170");
        }
    }
}
